//********************************************************
// PopupMenu.java
// Date: 4/17/2012
// Author: Jeff Xiong
//********************************************************
// Popup menu which get number of players
// ******************************************************
package ui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class GetNumPlayers implements  ItemListener {
	private static int _size = 0;
	private static JPanel play;
	private static JFrame f1 = new JFrame("Robber Knights");

	/**
	 * Gets the size of the number of players
	 * 
	 * @param none
	 * @return int number of players
	 */
	public int getSize()
	{
		f1.dispose();
		return _size;
	}
	
	/**
	 * Initialize frame display window to get number of players
	 * 
	 * @param none
	 * @return none
	 */
	public void startGame()
	{
		// Create and set up the window.
		f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Create and set up the content pane.		
		GetNumPlayers x = new GetNumPlayers();
		x.firstScreen(f1.getContentPane());
		
		// Display the window.
		f1.pack();
		f1.setVisible(true);
		f1.setLocationRelativeTo(null);
		while (_size == 0) {}
	}

	/**
	 * Make and add buttons and their functions to the_get_number_of_players
	 * windows
	 * 
	 * @param Container pane for content to be added to
	 * @return none
	 */
	public void firstScreen(Container pane)
	{

		// final String onePlayer="1 Player";
		final String twoPlayer = "2 Player";
		final String threePlayer = "3 Player";
		final String fourPlayer = "4 Player";

		JPanel comboBoxPane = new JPanel(); // use FlowLayout

		// Drop down menu
		String comboBoxItems[] = { twoPlayer, threePlayer, fourPlayer };
		
		// add string to new drop down menu
		JComboBox cb = new JComboBox(comboBoxItems);
		cb.setEditable(false);	
		cb.addItemListener(this);

		// add to panel
		comboBoxPane.add(cb);

		// new button

		JButton start1 = new JButton("Start ");
		JButton quit1 = new JButton(" Quit ");
		JButton start2 = new JButton("Start ");
		JButton quit2 = new JButton(" Quit ");
		JButton start3 = new JButton("Start ");
		JButton quit3 = new JButton(" Quit ");
		JButton start4 = new JButton("Start ");
		JButton quit4 = new JButton(" Quit ");

		/*********************** button ********************/
		// Create the "cards".
		JPanel onePlay = new JPanel();

		onePlay.add(start1);
		start1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("1  start");
				System.exit(0);
			}
		});
		onePlay.add(quit1);
		quit1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("1 quit");
				System.exit(0);
			}
		});

		/********************** button *********************/
		JPanel twoPlay = new JPanel();

		twoPlay.add(start2);
		start2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				_size = 2;
				System.out.println("2 player");
				// System.exit(0);
			}
		});

		twoPlay.add(quit2);
		quit2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// System.out.println("2 quit");
				System.exit(0);
			}
		});

		/******************** button ***********************/
		JPanel threePlay = new JPanel();

		threePlay.add(start3);
		start3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				_size = 3;
				System.out.println("3 player");
				
			}
		});

		threePlay.add(quit3);
		quit3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// System.out.println("3 quit");
				System.exit(0);
			}
		});

		/******************* button ************************/
		JPanel fourPlay = new JPanel();

		fourPlay.add(start4);
		start4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// System.out.println("4 start");
				
				_size = 4;
				System.out.println("4 player");
				
			}
		});

		fourPlay.add(quit4);
		quit4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// System.out.println("4 quit");
				System.exit(0);
			}
		});

		/*******************************************/
		// WELCOME
		JTextArea x = new JTextArea(3, 3);
		x.setEditable(false);
		x.append("     Welcome to ROBBER KNIGHTS       " + "\n");
		x.append("     Please select number of players or quit       " + "\n");

		play = new JPanel(new CardLayout());
		play.add(twoPlay, twoPlayer);
		play.add(threePlay, threePlayer);
		play.add(fourPlay, fourPlayer);

		Container contentPane = pane;

		/******************* Add to JFRAME ************************/
		contentPane.add(x, BorderLayout.PAGE_START);
		contentPane.add(comboBoxPane, BorderLayout.CENTER);
		contentPane.add(play, BorderLayout.PAGE_END);
	}

	/**
	 * handle events for combo box events
	 * 
	 * @param ItemEvent evt
	 * @return none
	 */
	public void itemStateChanged(ItemEvent evt)
	{
		// TODO
		CardLayout cl = (CardLayout) (play.getLayout());
		cl.show(play, (String) evt.getItem());
	}
}